package hu.mapro.gwtui.gxt.client.browser;

import hu.mapro.gwtui.gxt.client.modelkeyprovider.DefaultModelKeyAdapter;

import java.util.List;

import com.google.common.collect.Lists;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.form.AdapterField;
import com.sencha.gxt.widget.core.client.grid.CheckBoxSelectionModel;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;

public class GridListMultiSelect<T> extends AdapterField<List<T>> {

	ListStore<T> store;
	Grid<T> grid;
	
	
	@SuppressWarnings("unchecked")
	public GridListMultiSelect(ColumnModel<T> cm) {
		super(createWidget(cm));

		grid = (Grid<T>) getWidget();
		store = grid.getStore();

	}
	
	private static <T> Widget createWidget(ColumnModel<T> cm) {
		ListStore<T> store = new ListStore<T>(new DefaultModelKeyAdapter<T>());
		CheckBoxSelectionModel<T> selectionModel = new CheckBoxSelectionModel<T>(new IdentityValueProvider<T>());
		List<ColumnConfig<T, ?>> cms = Lists.newArrayList(); 
		cms.add(selectionModel.getColumn());
		cms.addAll(cm.getColumns());
		Grid<T> grid = new Grid<T>(store, new ColumnModel<T>(cms));
		grid.setSelectionModel(selectionModel);
		
		grid.setBorders(true);
		grid.getView().setForceFit(true);
		return grid;
	}

	@Override
	public void setValue(List<T> value) {
		grid.getSelectionModel().setSelection(value);
	}

	@Override
	public List<T> getValue() {
		return grid.getSelectionModel().getSelectedItems();
	}

	public void setSourceList(List<T> list) {
		store.replaceAll(list);
	}
}
